// ActionScript file
import com.model.Book_VO;
import com.model.InteractivePage;
import com.model.Model;
import com.pricope.miti.book.LargeBook;
import com.rubenswieringa.book.BookEvent;

import flash.events.Event;

import mx.binding.utils.BindingUtils;
import mx.controls.Alert;
import mx.controls.ComboBox;
import mx.controls.Label;
import mx.events.ItemClickEvent;


[Bindable]
public var book:LargeBook = null;
[Bindable]
private var _app:Model = Model.getIntstance();

public function init():void {
	//_app.addEventListener("bookChanged", recreateBook);
	if(_app.books && _app.books.length > 1) {
		addBooksComboBox();
	}
}

public function updateSoundPlayerData(url:String, title:String):void {
	mp3Player.media_location = url;
	mp3Player.transtitle = title;
	mp3Player.setMsg(title);
	mp3Player.autoPlay = false;
	mp3Player.play();
	mp3Player.stop();
	mp3Player.play();
}

public function recreateBook(e:Event):void {
	if(book) {
		removeChild(book);
	}
	book = null;
	book = createBook();
	
	for each(var page:InteractivePage in _app.currentBook.bookPages) {
		book.addChild(page);
	}	 
	addChildAt(book, 0);
	
}

private function createBook():LargeBook {
	var myBook:LargeBook = new LargeBook();
	myBook.width = 1000;
	myBook.height = 500;
	
	myBook.autoFlipDuration = 600;
	myBook.easing = 0.7;
	myBook.hover = true;
    myBook.addEventListener(BookEvent.PAGE_TURNED, function (e:BookEvent):void {
		var url:String = IslamicBook.BACKEND_URL
						 + "books/" + Model.getIntstance().currentBook.bookId 
						 + "/pages/" + book.currentIndex + '.mp3';
		var second:Number = book.currentIndex + 1;
		var url2:String = IslamicBook.BACKEND_URL
						 + "books/" + Model.getIntstance().currentBook.bookId 
						 + "/pages/" + second + '.mp3';
		mp3Player.addSound(url, "Page " + book.currentIndex);
		mp3Player.addSound(url2, "Page " + second, false);
		mp3Player.autoPlay = true;
		mp3Player.play();
//		mp3Player.stop();			 
//		mp3Player.play();
		//updateSoundPlayerData(url,"Page " + book.currentIndex);
		} );
	return myBook;
}
private function onButtonBarClicked(event:ItemClickEvent):void {
	switch(event.index) {
		case 0: // First
			book.jumpToPage(0);
			break;
		case 1: // Last
			book.jumpToPage(book.pageArray.length);
			break;
		case 2: // prev
			book.prevPage();
			break;
		case 3: // next
			book.nextPage();
			break;
	}
}

public function addBooksComboBox():void {
	var label:Label = new Label();
	label.text = "Books"; 
	
	this.controlBar.addChild(label);
	
	var comboBox:ComboBox = new ComboBox();
	comboBox.labelField='title';
	//comboBox.dataProvider = _app.courses;  
	BindingUtils.bindProperty(comboBox, "dataProvider", _app, "books");
	comboBox.addEventListener("change", handleBookChanged);
	//comboBox.addEventListener("dataChange", handleDataChange);
	this.controlBar.addChild(comboBox);
}

private function handleDataChange(e:Event):void {
	ComboBox(e.target).selectedIndex = 2;
}
private function handleBookChanged(e:Event):void {
	var book:Book_VO = Book_VO(ComboBox(e.target).selectedItem);
	
	if(!book) {
		Alert.show("Error: there is no book for this course");	
	} else {
		_app.currentBook = book;		
	}
}